var lcff_options = {
    onLoad : function() {
        LockCrypt.loadPrefs();
        document.getElementById("txtExcluded").value = LockCrypt.excludeList.join("\n");
        var passwordManager = Components.classes["@mozilla.org/login-manager;1"]
                                        .getService(Components.interfaces.nsILoginManager);
        var numLogins = passwordManager.countLogins("","","");
        dump(numLogins+" logins\n");
        document.getElementById("lblNumPasswords").value = LockCrypt.getFormattedString("numPasswordsFirefox", [ numLogins ]);
    },
    saveOptions : function() {
        var excludes = document.getElementById("txtExcluded").value;
        var excludeAry = [];
        var tempAry = excludes.split("\n");
        for(var i=0;i<tempAry.length;i++) {
            var currentExclude = tempAry[i].trim();
            if(currentExclude.length > 0) {
                excludeAry[excludeAry.length] = tempAry[i];
            }
        }
        LockCrypt.prefs.setCharPref("excludeList", JSON.stringify(excludeAry));
        close();
    },
    importFromLockCrypt : function() {
        var listener = {
            finished : function(data) {
                var responseData = JSON.parse(data);
                if(typeof responseData.error != 'undefined') {
                    alert(decodeURIComponent(responseData.error));
                } else {
                    var passwordManager = Components.classes["@mozilla.org/login-manager;1"]
                                                    .getService(Components.interfaces.nsILoginManager);
                    var nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
                                                                 Components.interfaces.nsILoginInfo,
                                                                 "init");

                    var accounts = responseData.accounts;
                    var existingLogins;
                    var thisExistingLogin;
                    var accountExists;
                    var thisAccount;
                    var hostname;
                    var numImported = 0;
                    for(var i=0;i<accounts.length;i++) {
                        accountExists = false;
                        thisAccount = accounts[i];
                        hostname = decodeURIComponent(thisAccount.hostname);
                        //dump("Account for: "+hostname+"\n");
                        existingLogins = passwordManager.findLogins({}, hostname, null, hostname);
                        for(var j=0;j<existingLogins.length;j++) {
                            thisExistingLogin = existingLogins[j];
                            //dump("Hostname exists, type is ");
                            //dump(thisExistingLogin+" existing username is ");
                            //dump(thisExistingLogin.username+", new username is ");
                            //dump(thisAccount.username+"\n");
                            if(thisExistingLogin.username == thisAccount.username) {
                                accountExists = true;
                                break;
                            }
                        }
                        if(!accountExists) {
                            try{
                                passwordManager.addLogin(new nsLoginInfo(hostname, 
                                                                         null, 
                                                                         hostname, 
                                                                         decodeURIComponent(thisAccount.username), 
                                                                         decodeURIComponent(thisAccount.password),
                                                                         decodeURIComponent(thisAccount.usernameField),
                                                                         decodeURIComponent(thisAccount.passwordField)));
                                numImported++;
                            } catch(ex) {
                                dump("#"+numExported+": "+ex+"\n");
                            }
                        }
                    }
                    document.getElementById("transferProgress").hidden = true;
                    alert(LockCrypt.getFormattedString("numImported", [numImported]));
                    var numLogins = passwordManager.countLogins("","","");
                    document.getElementById("lblNumPasswords").value = LockCrypt.getFormattedString("numPasswordsFirefox", [ numLogins ]);
                }
            }
        };
        document.getElementById("transferProgress").hidden = false;
                
        var data = {
            "type" : "EXPORT" // import from Firefox, but export to LockCrypt
        };
        LockCrypt.sendReceive('localhost',1556,JSON.stringify(data),listener);
    },
    exportToLockCrypt : function() {
        var listener = {
            finished : function(data) {
                document.getElementById("transferProgress").hidden = true;
                var responseData = JSON.parse(data);
                if(typeof responseData.error != 'undefined') {
                    alert(decodeURIComponent(responseData.error));
                } else {
                    alert(decodeURIComponent(responseData.response));
                }
            }
        };
        document.getElementById("transferProgress").hidden = false;
        var passwordManager = Components.classes["@mozilla.org/login-manager;1"]
                                        .getService(Components.interfaces.nsILoginManager);

        var logins = passwordManager.getAllLogins({});
        var pass;
        var accounts = [];
        for(var i=0;i<logins.length;i++) {
            pass = logins[i];
            accounts[accounts.length] = {
                url : encodeURIComponent(pass.hostname),
                username : encodeURIComponent(pass.username),
                usernameID : encodeURIComponent(pass.usernameField),
                password : encodeURIComponent(pass.password),
                passwordID : encodeURIComponent(pass.passwordField)
            };
        }
        var data = {
            "type" : "IMPORT", // export from Firefox, but import to LockCrypt
            "accounts" : accounts
        };
        LockCrypt.sendReceive('localhost',1556,JSON.stringify(data),listener);
    }
};